package cn.jiangjiesheng.bootstrap.commons.context;

import cn.jiangjiesheng.bootstrap.commons.exception.BizzException;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * Created by dev@jiangjiesheng.cn on 2021/9/26  <br/>
 *
 * @author dev@jiangjiesheng.cn
 */
@Slf4j
public class EntContext {

    private static final ThreadLocal<String> entThreadLocal = new ThreadLocal<>();

    public static void setEntId(@NonNull String entId) {
        entThreadLocal.set(entId);
    }

    public static String getEntId() {
        if (isSet()) {
            return entThreadLocal.get();
        } else if (UserInfoContext.isUserLogin()) {
            return UserInfoContext.getUserInfo().getContainerId();
        } else {
            throw new BizzException("请登录，或者通过GET参数传入entId/containerId字段");
        }
    }

    public static boolean isSet() {
        return StringUtils.isNotBlank(entThreadLocal.get());
    }

    public static void reset(){
        entThreadLocal.remove();
    }
}
